//
//  CAForumController.m
//  cascade-realm
//
//  Created by QSC on 20/5/24.
//

#import "CAForumController.h"
#import "CAForumDetailController.h"
#import "CAForumCell.h"
@interface CAForumController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UIView * centerView;

@property (nonatomic,strong) NSArray * users;

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSArray * dataArray;
@end

@implementation CAForumController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navView.hidden = YES;
    
    [self setUpView];
    
    [self showAlertPic];
    

    
    [CAHomeUser getForumUsers:^(NSArray * _Nonnull array) {
        self.users = array;
        [self.tableView reloadData];
    }];
  
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear: animated];
    
    self.dataArray = [CADataModel getPuhlishForum];
    
    [self.tableView reloadData];
}

- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor clearColor];
        [_tableView registerClass:[CAForumCell class] forCellReuseIdentifier:@"CAForumCell"];
      
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
  
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    return  _tableView;
}

- (void)setUpView{
    
    UIImageView * titleImgv = [[UIImageView alloc] initWithImage:Img(@"title_forum")];
    [self.view addSubview:titleImgv];
  
    [titleImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(263);
        make.height.mas_equalTo(77);
        make.top.mas_equalTo(kNavigationBarHeight + 10);
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(titleImgv.mas_bottom).offset(34);
        make.bottom.equalTo(self.view).offset(-kTabBarHeight);
    }];
    

    UIImageView * bgImgv = [[UIImageView alloc] initWithImage:Img(@"bg_home")];
    bgImgv.contentMode = UIViewContentModeScaleAspectFit;
    [self.view insertSubview:bgImgv belowSubview:self.tableView];
    [bgImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(ScreenH * 0.5);
        make.bottom.equalTo(self.view).offset(-kTabBarHeight - 10);
    }];
}




- (void)showAlertPic{
    
    BOOL didShowForumAlert =  [[NSUserDefaults standardUserDefaults] objectForKey:@"didShowForumAlert"];
    
    if(didShowForumAlert){
        return;
    }
    
    UIImageView * imagev = [[UIImageView alloc] initWithImage:Img(@"forum_alert")];
    CAAlertView * alert = [CAAlertView ShowAlertWithContent:imagev];
    alert.isDismissible = true;
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"didShowForumAlert"];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CAForumCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CAForumCell"];
    
    if (indexPath.row == 0) {
        cell.users = self.users;
    }else{
        cell.model = self.dataArray[indexPath.row -1];
    }
    
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CAForumDetailController * vc = [CAForumDetailController new];
    if (indexPath.row == 0) {
        BOOL didJoin = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"DidJoinForum--%@",CAUserInfo.shared.userInfo.userId]];
        
        if (!didJoin) {
            return;
        }
        
        CAHomeUser * u = self.users.firstObject;
        NSString * key = u.dataKeys.firstObject;//取第一个用户第一个作品
        NSDictionary * dic = [DBTool getData:key];
        vc.model = [CADataModel mj_objectWithKeyValues:dic];
    }else{
        vc.model = self.dataArray[indexPath.row -1];
    }
   
    [self.navigationController pushViewController:vc animated:YES];
    
}


@end
